Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find and find! methods to Indexable #15552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

punteek
Copy link

@punteek punteek commented Mar 13, 2025

Added implementation and tests for find and find! in Indexable, as discussed in this issue: #14642

src/indexable.cr Outdated
Comment on lines 836 to 838
offset += size if offset < 0
return nil if offset < 0
return (index(offset) { |i| yield i }).try { |i| unsafe_fetch(i) } || raise Enumerable::NotFoundError.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense to follow the pattern of the other ! bang methods:

Suggested change
offset += size if offset < 0
return nil if offset < 0
return (index(offset) { |i| yield i }).try { |i| unsafe_fetch(i) } || raise Enumerable::NotFoundError.new
find(offset) || raise Enumerable::NotFoundError.new

Otherwise find! could return nil if given an invalid offset while the other methods like index! would raise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, addressed!

Copy link
Member

@Blacksmoke16 Blacksmoke16 Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is entirely addressed. If you do something like collection.find! -1 that'll return nil as it stands. Which we likely don't want to happen for this bang version.

NVM, I see the logic that handles that now. But the return nil still concerns me as wouldn't that make the return type T? which again makes find! no different than find?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants